package server_commands;

import java.sql.SQLException;
import java.util.StringTokenizer;

import server.ServerSocketConnection;
import connection_modul.DatabaseConnection;

public class CHPSWDCommand extends Command {
	
	
	public CHPSWDCommand(String parameter) {
		setCode(CHPSWD_CMD);
		setParameter(parameter);
	}
	
	/**
	 * Vrsi se upis novog passworda korisnika u bazu.
	 * 
	 * @param connection - konekcija ka klijentu
	 * @param dbConnection - objekat za rad sa bazom
	 * 
	 */
	public boolean executeCommand(ServerSocketConnection connection, DatabaseConnection dbConnection) {
		
		StringTokenizer string = new StringTokenizer(getParameter());
		
		String username = string.nextToken();
		String newPassword = string.nextToken();
		
		try {
			dbConnection.doUpdateStatement("UPDATE tblUsers SET tblUsers.password = '" +
								newPassword + "' WHERE tblUsers.username = '" + username + "'");
			
		} catch (SQLException e) {
			e.printStackTrace();
			dbConnection.doRollBack();
			return false;
		}
		
		connection.setMessage("100 Password changed.");		
			
		dbConnection.doCommit();
		
		return true;
	}

}
